package com.eric.r2d2.urlbase;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

public class UpdateR2D2 {

	public static void main(String[] args) {
		if (args.length < 1) {
			System.out.println("Usage:File_in");
			System.exit(1);
		}
		
		//URL
		File file_in = new File(args[0]);
		File file_out = new File(file_in.getAbsolutePath()+".updated");
		BufferedReader br = null;
		PrintWriter bw = null;
		try {
			 br = new BufferedReader(new FileReader(file_in));
			bw = new PrintWriter(new FileWriter(file_out));
				String line;
			// 将目标url文件读取到queue里
				while ((line = br.readLine()) != null) {
					JSONObject json = JSON.parseObject(line);
					JSONObject inner = new JSONObject();
					
					String d = json.getString("crawl_date");
					long epoch = 0;
					try {
						epoch = (new SimpleDateFormat("yyyyMMdd HH:mm:ss").parse(d+" 00:00:00").getTime() + 28800000) ;
					} catch (ParseException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					inner.put("$date", epoch);
					json.put("crawl_date",inner);
					
					try
					{
						bw.print(json);
						bw.print("\n");
					}catch (java.lang.ArrayIndexOutOfBoundsException e) {
						e.printStackTrace();
					}
					
				}
				br.close();
				bw.close();
								
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
		
				
	}

}
